﻿using System;
using System.Reflection;

namespace PGMapper
{
    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
    public class ColumnAttribute : Attribute
    {
        public string Name { get; private set; }
        public bool IsSerial { get; set; }
        public bool IsPrimaryKey { get; set; }
        public bool IsUnique { get; set; }

        /// <summary>
        /// A shortcut for both IsSerial and IsPrimaryKey.
        /// </summary>
        public bool IsIdentity
        {
            get { return IsSerial && IsPrimaryKey; }
            set { IsSerial = IsPrimaryKey = value; }
        }

        public ColumnAttribute(string name)
        {
            this.Name = name;
        }

        public static ColumnAttribute Find(MemberInfo property)
        {
            object[] atts = property.GetCustomAttributes(typeof(ColumnAttribute), true);
            return atts.Length > 0 ? (ColumnAttribute)atts[0] : null;
        }
    }
}
